package com.liu.service.impl;

import com.liu.mapper.UserMapper;
import com.liu.pojo.User;
import com.liu.service.UserService;
import com.liu.utils.Md5Util;
import com.liu.utils.ThreadLocalUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }

    @Override
    public void register(@Param("username") String username, @Param("password") String password) {
//        参数有密码，在这里实行一个加密的处理
        String md5String = Md5Util.getMD5String(password);
        userMapper.add(username, md5String);
    }

    @Override
    public void updata(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updataAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updataAvatar(avatarUrl, id);
    }

    @Override
    public void updataPwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updataPwd(Md5Util.getMD5String(newPwd), id);
    }
}
